package psn.hui.sys.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import psn.hui.sys.constant.SysConstant;
import psn.hui.sys.domain.User;
import psn.hui.sys.service.RoleService;
import psn.hui.sys.service.UserService;
import psn.hui.sys.utils.DataGridView;
import psn.hui.sys.utils.JsonResult;
import psn.hui.sys.vo.UserVo;

/**
 * 	用户管理控制器
 * 
 * @author hui
 *
 */
@RestController
@RequestMapping("user")
public class UserController {
	
	@Autowired	
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	/**
	 * 	获取用户列表，分页，查询
	 */
	@RequestMapping("userList")
	public DataGridView userList(UserVo userVo) {
		List<User> userlist = null;
		Page<User> page = PageHelper.startPage(userVo.getPage(), userVo.getLimit());
		userlist = userService.queryAllUserForList(userVo);
		return  new DataGridView(page.getTotal(), userlist);
	}
	/**
	 * 	添加用户
	 * @param user
	 * @return
	 */
	@RequestMapping("addUser")
	public JsonResult addUser(User user) {
		try {
			int rs = userService.insertSelective(user);
			if(rs > 0) {
				return SysConstant.ADD_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.ADD_ERROR;
	}
	/**
	 * 	修改用户
	 * @param user
	 * @return
	 */
	@RequestMapping("updateUser")
	public JsonResult updateUser(User user) {
		System.out.println("updateUser:available:"+user.getAvailable());
		try {
			int rs = userService.updateByPrimaryKeySelective(user);
			if(rs > 0) {
				return SysConstant.UPDATE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.UPDATE_ERROR;
	}
	/**
	 * 	根据id删除用户
	 * @param userid
	 * @return
	 */
	@RequestMapping("deleteById")
	public JsonResult deleteById(Integer userid) {
		try {
			int rs = userService.deleteById(userid);
			if(rs > 0) {
				return SysConstant.DELETE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.DELETE_ERROR;
	}
	/**
	 * 	根据用户id批量删除用户数据，有关联删除
	 * @param userVo
	 * @return
	 */
	@RequestMapping("batchDeleteUser")
	public JsonResult batchDeleteUser(UserVo userVo) {
		try {
			int rs = userService.batchDeleteUserByIds(userVo.getIds());
			if(rs > 0) {
				return SysConstant.DELETE_SUCCESS;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.DELETE_ERROR;
	}
	
	/**
	 *	重置用户密码
	 * @param userid
	 * @return
	 */
	@RequestMapping("resetUserPwd")
	public JsonResult resetUserPwd(Integer userid) {
		try {
			int rs = userService.resetUserPwd(userid);
			if(rs > 0) {
				return SysConstant.RESET_SUCCESS;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.RESER_ERROR;
	}
	/**
	 * 	检查登录名loginname是否可用
	 * @param loginname
	 * @return
	 */
	@RequestMapping("checkLoginName")
	public JsonResult checkLoginName(String loginname) {
		if(userService.checkLoginName(loginname) > 0) {
			return SysConstant.USER_LOGINNAME_ERROR;
		}
		return SysConstant.USER_LOGINNAME_SUCCESS;
	}
	
	/**
     * 	初始化角色数据表格，根据用户id选中
     * @param userid
     * @return
     */
	@RequestMapping("initRoleTableByUserId")
    public DataGridView initRoleTableByUserId(Integer userid) {
    	try {
    		return roleService.initRoleTableByUserId(userid);
    	}catch (Exception e) {
			e.printStackTrace();
		}
    	return new DataGridView();
    }
	
	/**
	 * 	保存分配用户角色数据（用户角色关系表）,会先删除该用户旧的分配数据
	 * @param userVo
	 * @return
	 */
	@RequestMapping("saveUserRole")
	public JsonResult saveUserRole(UserVo userVo) {
		try {
			if(userService.saveUserRole(userVo) > 0) {
				return SysConstant.DISTRIBUTE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.DISTRIBUTE_ERROR;
	}
	
	/**
	 * 	用户修改密码
	 * @param user
	 * @param password
	 * @return
	 */
	@RequestMapping("changePwd")
	public JsonResult changePwd(User user, String password) {
		System.out.println("loginname:"+user.getLoginname()+",pwd:"+user.getPwd()+",password:"+password);
		try {
			if(userService.changePwd(user, password)) {
				return SysConstant.UPDATE_SUCCESS;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.UPDATE_ERROR;
		
	}
}
